package com.sql.azarias.ui.fragment;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sql.azarias.R;
import com.sql.azarias.adapter.LiveeAdapter;
import com.sql.azarias.base.BaseMvpFragment;
import com.sql.azarias.entity.LiveEntity_;
import com.sql.azarias.ui.fragment.Presenter.LivePresenter;
import com.sql.azarias.ui.fragment.view.LiveView;
import com.sql.azarias.views.ToastView;

import butterknife.Bind;

/**
 * Created by ShangQiangLong on 2017/3/1.
 */

public class LiveFragment extends BaseMvpFragment<LiveView,LivePresenter> implements LiveView ,SwipeRefreshLayout.OnRefreshListener{
    private Context context;
    @Bind(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @Bind(R.id.swipe_refresh_ly)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @Override
    public LivePresenter initPresenter() {
        return new LivePresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_live;
    }

    @Override
    protected void initInjector() {


    }

    @Override
    protected void initEventAndData() {
        context=getActivity();
        mSwipeRefreshLayout.setOnRefreshListener(this);
        presenter.requestString(context);
    }

    @Override
    public void setEntityItem(LiveEntity_ entity) {
        mSwipeRefreshLayout.setRefreshing(false);
        final LiveeAdapter liveeAdapter = new LiveeAdapter(entity,context);
        GridLayoutManager layout = new GridLayoutManager(context, 2);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {

                return liveeAdapter.getSpanSize(position);
            }
        });

        mRecyclerView.setLayoutManager(layout);
        mRecyclerView.setAdapter(liveeAdapter);
    }

    @Override
    public void showMassage(String massage) {
        ToastView.getInstance().show(massage, context);
    }

    @Override
    public void onRefresh() {
        presenter.requestString(context);
    }
}
